﻿using System;
using System.Collections.ObjectModel;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using Common.Constants;
using Entities;
using DataAccess;
using System.Linq;
namespace Business
{
    public class OrganizationBusiness
    {
        #region ThinhDV
        /// <summary>
        /// Gets the instance.
        /// </summary>
        /// <value>
        /// The instance.
        /// </value>
        public static OrganizationBusiness Instance { get; private set; }


        static OrganizationBusiness() { Instance = new OrganizationBusiness(); }

        public Collection<Organisation> ListOrganisation(string keyFilter, bool isActive)
        {
            const string digitPattern = "^[0-9]";
            const string aePattern = "^[a-eA-E]";
            const string fjPattern = "^[f-jF-J]";
            const string knPattern = "^[k-nK-N]";
            const string orPattern = "^[o-rO-R]";
            const string svPattern = "^[s-vS-V]";
            const string wzPattern = "^[w-zW-Z]";
            Collection<Organisation> lstOrganisation;
            try
            {
                lstOrganisation = OrganizationDataAccess.Instance.ListOrganization(isActive);
            }
            catch (SqlException)
            {

                lstOrganisation = null;
            }

            if (lstOrganisation != null)
                switch (keyFilter)
                {
                    case "All":
                        
                            return lstOrganisation;
                       

                    case "09":
                        
                            return Filter(digitPattern, lstOrganisation);
                        
                    case "ae":
                        
                            return Filter(aePattern, lstOrganisation);
                        
                    case "fj":
                       
                            return Filter(fjPattern, lstOrganisation);
                        
                    case "kn":
                        
                            return Filter(knPattern, lstOrganisation);
                       
                    case "or":
                       
                            return Filter(orPattern, lstOrganisation);
                       
                    case "sv":
                       
                            return Filter(svPattern, lstOrganisation);
                       
                    case "wz":
                       
                            return Filter(wzPattern, lstOrganisation);
                        
                }
            return null;
        }
        private Collection<Organisation> Filter(string pattern, Collection<Organisation> lstOrg)
        {
            var query = from o in lstOrg
                        where checkPattern(pattern, o.OrgName)
                        select o;
            return new Collection<Organisation>(query.ToList());
        }
        private bool checkPattern(string pattern, string target)
        {
            var myRegex = new Regex(pattern);
            var match = myRegex.Match(target);
            return match.Success;
        }

        public Collection<Organisation> PagingOrg(int pageNumber, int pageSize, Collection<Organisation> organisations)
        {
            if (organisations != null)
            {
                if (pageSize > 0)
                {
                    var totalpage = organisations.Count % pageSize == 0 ?organisations.Count / pageSize : organisations.Count / pageSize + 1;
                    if (pageNumber <= totalpage && pageNumber > 0)

                        return new Collection<Organisation>(organisations.Skip((pageNumber - 1) * pageSize).Take(pageSize).ToList());

                }
                return null;
            }
            return null;

        }
        public bool MarkInActive(string orgId)
        {
            try
            {
                return OrganizationDataAccess.Instance.MarkInActive(orgId)>0;
            }
            catch (SqlException)
            {

                return false;
            }
        }
        public Collection<Organisation> OrganisationOrderByContact(string keyFilter, bool orderMode)
        {
            const string digitPattern = "^[0-9]";
            const string aePattern = "^[a-eA-E]";
            const string fjPattern = "^[f-jF-J]";
            const string knPattern = "^[k-nK-N]";
            const string orPattern = "^[o-rO-R]";
            const string svPattern = "^[s-vS-V]";
            const string wzPattern = "^[w-zW-Z]";
            Collection<Organisation> lstOrganisation;
            try
            {
                lstOrganisation = OrganizationDataAccess.Instance.OrganisationOrderByContact(orderMode);
            }
            catch (SqlException)
            {

                lstOrganisation = null;
            }

            if (lstOrganisation != null)
                switch (keyFilter)
                {
                    case "All":

                        return lstOrganisation;


                    case "09":

                        return Filter(digitPattern, lstOrganisation);

                    case "ae":

                        return Filter(aePattern, lstOrganisation);

                    case "fj":

                        return Filter(fjPattern, lstOrganisation);

                    case "kn":

                        return Filter(knPattern, lstOrganisation);

                    case "or":

                        return Filter(orPattern, lstOrganisation);

                    case "sv":

                        return Filter(svPattern, lstOrganisation);

                    case "wz":

                        return Filter(wzPattern, lstOrganisation);

                }
            return null;
        }
        public Collection<Organisation> OrganisationOrderByAddress(string keyFilter, bool orderMode)
        {
            const string digitPattern = "^[0-9]";
            const string aePattern = "^[a-eA-E]";
            const string fjPattern = "^[f-jF-J]";
            const string knPattern = "^[k-nK-N]";
            const string orPattern = "^[o-rO-R]";
            const string svPattern = "^[s-vS-V]";
            const string wzPattern = "^[w-zW-Z]";
            Collection<Organisation> lstOrganisation;
            try
            {
                lstOrganisation = OrganizationDataAccess.Instance.OrganisationOrderByAddress(orderMode);
            }
            catch (SqlException)
            {

                lstOrganisation = null;
            }

            if (lstOrganisation != null)
                switch (keyFilter)
                {
                    case "All":

                        return lstOrganisation;


                    case "09":

                        return Filter(digitPattern, lstOrganisation);

                    case "ae":

                        return Filter(aePattern, lstOrganisation);

                    case "fj":

                        return Filter(fjPattern, lstOrganisation);

                    case "kn":

                        return Filter(knPattern, lstOrganisation);

                    case "or":

                        return Filter(orPattern, lstOrganisation);

                    case "sv":

                        return Filter(svPattern, lstOrganisation);

                    case "wz":

                        return Filter(wzPattern, lstOrganisation);

                }
            return null;
        }
        
        #endregion

        #region Hieutt5
        public bool InsertOrganisation(Organisation organization)
        {
            try
            {
                if (!CheckOrganisationNameExist(organization.OrgName))
                {
                    var result = OrganizationDataAccess.Instance.InsertOrganisation(organization);
                    return result > 0;
                }
            }
            catch(SqlException ex)
            {
                return false;
            }
            return false;

        }

        public Collection<Address> CheckPostCodeExist(string postCode)
        {
            var result = AddressDataAccess.Instance.GetAddressByPostcode(postCode);
            return result;
        }
        
        public bool CheckOrganisationNameExist(string organisationName)
        {
            var result = OrganizationDataAccess.Instance.GetOrganisationByName(organisationName);
            return result != null;
        }

        public bool CheckOrganisationNameUpdate(string organisationName,Guid orgId)
        {
            var result =
                OrganizationDataAccess.Instance.ListOrganization(false).SingleOrDefault(
                    c => c.OrgId != orgId && c.OrgName == organisationName);
            return result != null;
        }

        public bool UpdateOrganisation(Organisation organisation)
        {
            try
            {
                if (!CheckOrganisationNameUpdate(organisation.OrgName, organisation.OrgId))
                {
                    var result = OrganizationDataAccess.Instance.UpdateOrganization(organisation);
                    return result > 0;
                }
            }
            catch (SqlException)
            {
                return false;
            }
           
            return false;
        }

        public Organisation GetOrganisationById(string organisationId)
        {
            return OrganizationDataAccess.Instance.GetOrganisationById(organisationId);
        }

        #region Get Reference Data
        public Collection<ReferenceData> GetOrganisationSpecialisation()
        {
            const int refCode = (int)RefCode.OrganisationSpecialisation;
            return ReferenceDataDataAccess.Instance.GetReferenceData(refCode);
        }

        public Collection<ReferenceData> GetServiceDisabilitiesCapabilities()
        {
            const int refCode = (int)RefCode.ServiceDisabilitiesCapabilities;
            return ReferenceDataDataAccess.Instance.GetReferenceData(refCode);
        }

        public Collection<ReferenceData> GetServiceBarriersCapabilities()
        {
            const int refCode = (int)RefCode.ServiceBarriersCapabilities;
            return ReferenceDataDataAccess.Instance.GetReferenceData(refCode);
        }

        public Collection<ReferenceData> GetServiceBenefitsCapabilities()
        {
            const int refCode = (int)RefCode.ServiceBenefitsCapabilities;
            return ReferenceDataDataAccess.Instance.GetReferenceData(refCode);
        }

        public Collection<ReferenceData> GetServicePersonalCircumstanceCapabilities()
        {
            const int refCode = (int)RefCode.ServicePersonalCircumstanceCapabilities;
            return ReferenceDataDataAccess.Instance.GetReferenceData(refCode);
        }

        public Collection<ReferenceData> GetServiceEthnicityCapabilities()
        {
            const int refCode = (int)RefCode.ServiceEthnicityCapabilities;
            return ReferenceDataDataAccess.Instance.GetReferenceData(refCode);
        }

        public Collection<ReferenceData> GetAccreditation()
        {
            const int refCode = (int)RefCode.Accreditation;
            return ReferenceDataDataAccess.Instance.GetReferenceData(refCode);
        }

        public Collection<ReferenceData> GetEoiProgrammes()
        {
            const int refCode = (int)RefCode.EoiProgrammes;
            return ReferenceDataDataAccess.Instance.GetReferenceData(refCode);
        }

        public Collection<ReferenceData> GetEoiServices()
        {
            const int refCode = (int)RefCode.EoiServices;
            return ReferenceDataDataAccess.Instance.GetReferenceData(refCode);
        }

        #endregion

        #endregion
    }
}
